package org.example.iris.utils;


import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import java.util.Random;

@Component
public class EmailCodeUtil {

    @Autowired
    JavaMailSenderImpl mailSender;

    public String sendCode(String email) throws Exception {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
        Random random = new Random();
        int code = 100000 + random.nextInt(900000);
        String codeNum = String.valueOf(code);
        //标题
        helper.setSubject("您的验证码为："+codeNum);
        //内容
        helper.setText("有效时间为5分钟！请尽快使用！");
        helper.setText("实习招聘系统IRIS");
        helper.setText("您的验证码为："+"<h2>"+codeNum+"</h2>",true);
        //邮件接收者
        helper.setTo(email);
        //邮件发送者，必须和配置文件里的一样，不然授权码匹配不上
        helper.setFrom("2733904699@qq.com");
        //log.info("验证码为"+codeNum);
        mailSender.send(mimeMessage);
        //log.info("邮件发送成功！");
        return codeNum;
    }
}
